<?php

/**
 * site actions.
 *
 * @package    reduceri-cadouri
 * @subpackage site
 * @author     Your name here
 */
class siteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->Sites = SitePeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new SiteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new SiteForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Site = SitePeer::retrieveByPk($request->getParameter('id')), sprintf('Object Site does not exist (%s).', $request->getParameter('id')));
    $this->form = new SiteForm($Site);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Site = SitePeer::retrieveByPk($request->getParameter('id')), sprintf('Object Site does not exist (%s).', $request->getParameter('id')));
    $this->form = new SiteForm($Site);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Site = SitePeer::retrieveByPk($request->getParameter('id')), sprintf('Object Site does not exist (%s).', $request->getParameter('id')));
    $Site->delete();

    $this->redirect('site/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $Site = $form->save();

      $this->redirect('site/edit?id='.$Site->getId());
    }
  }
  
  public function executeUrls(sfWebRequest $request)
  {
    $this->forward404Unless($this->site = SitePeer::retrieveByPK($request->getParameter('site_id')));
    
    $site_url = new SiteUrl();
    $site_url->setSite($this->site);
    $this->form = new SiteUrlForm($site_url);
    
    if($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('site_url'));
      if($this->form->isValid())
      {
        $site_url = $this->form->save();
        
        return $this->redirect('site/urls?site_id='.$this->site->getId());
      }
    }
    
    $this->site_urls = $this->site->getSiteUrls();
  }
  
  public function executeDeleteUrl(sfWebRequest $request)
  {
    $this->forward404Unless($this->site_url = SiteUrlPeer::retrieveByPK($request->getParameter('id')));
    
    $this->site_url->delete();
      
    return $this->redirect('site/urls?site_id='.$this->site_url->getSiteId());
  }
  
  public function executeParse(sfWebRequest $request)
  {
    $this->forward404Unless($this->site = SitePeer::retrieveByPK($request->getParameter('site_id')));
    
    switch($this->site->getId())
    {
      case 1: // fundeal
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseFundeal($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                try
                {
                  $offer_city->delete();
                }
                catch(Exception $e)
                {
                  var_dump($e->getMessage());
                }
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
      case 2:
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseZumzi($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                try
                {
                  $offer_city->delete();
                }
                catch(Exception $e)
                {
                  var_dump($e->getMessage());
                }
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
      case 3:
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseGroupon($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                $offer_city->delete();
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
      case 4: // goldendeals
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseGoldendeals($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                try
                {
                  $offer_city->delete();
                }
                catch(Exception $e)
                {
                  var_dump($e->getMessage());
                }
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
      case 5: // kuponiada
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseKuponiada($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                $offer_city->delete();
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
      case 6: // coolbuy
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseCoolbuy($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                try
                {
                  $offer_city->delete();
                }
                catch(Exception $e)
                {
                  var_dump($e->getMessage());
                }
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
      case 7: // topdeals
        foreach($this->site->getSiteUrls() as $site_url)
        {
          $offers = SiteParser::parseTopdeals($site_url->getUrl());
          foreach($offers as $offer)
          {
            if($offer)
            {
              foreach($offer->getOfferCitys() as $offer_city)
              {
                try
                {
                  $offer_city->delete();
                }
                catch(Exception $e)
                {
                  var_dump($e->getMessage());
                }
              }
              $offer_city = new OfferCity();
              $offer_city->setCityId($site_url->getCityId());
              $offer_city->setOfferId($offer->getId());
              $offer_city->save();
              $offer->setSiteId($this->site->getId());
              $offer->save();
            }
          }
        }
        break;
    }
    
    return $this->redirect('site/index');
   }
}
